package com.snbc.string;

import java.util.Random;


public class StringBufferdaa {
    public StringBufferdaa(String name) {
    }

    public static void main(String[] args) {
        String str = random(10);
        String word1 = "";
        long start = System.currentTimeMillis(); // 开始时间
        for (int i = 0; i < 10000; i++) {
            word1 += str; // 使用String+连接
        }
        long end = System.currentTimeMillis(); // 结束时间
       // System.out.println(System.out.printf("使用String+的方式,耗时%d毫秒.%n", end - start));
        // System.out.println(word1);
        // if (i % 10 == 0) System.out.println("\n");

        java.lang.StringBuffer stringBuffer = new java.lang.StringBuffer();
        long start2 = System.currentTimeMillis(); // 开始时间

        for (int i = 0; i < 10000; i++) {
            stringBuffer.append(str);// 使用StringBuffer连接
        }
        long end2 = System.currentTimeMillis(); // 结束时间
        System.out.printf("使用StringBuffer的方式,,耗时%d毫秒.", end2 - start2);
        System.out.println(stringBuffer);
    }

    private static String random(int length) {

        char[] array = new char[length];
        int min = '0';
        int max = 'z' + 1;
        for (int i = 0; i < array.length; i++) {
            while (true) {
                char mark = (char) ((Math.random() * (max - min) + min));
                if (Character.isDigit(mark) || Character.isLetter(mark)) {
                    array[i] = mark;
                    break;
                }
            }
        }
        String result = new String(array);
        return result;

    }

}
